package structure;

public class DoubleLinkedList {
    private static class Node {
        int val;
        Node prev;
        Node next;

        Node(int val) {
            this.val = val;
        }
    }

    private Node head; // 链表头部
    private Node last; // 链表尾部

    public void addFirst(int data) {
        Node node = new Node(data);
        if (head == null) {
            head = node;
            last = node;
        }else {
            node.next = head;
            head.prev = node;
            head = node;
        }
    }

    public void addLast(int data) {
        Node node = new Node(data);
        if (head == null) {
            head = node;
            last = node;
        }else {
            last.next = node;
            node.prev = last;
            last = node;
        }
    }

    public void addIndex(int data, int index) {
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        if (index < 0 || index > size()) {
            return;
        }
        Node cur = head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        Node node = new Node(data);

        node.prev = cur.prev;
        node.next = cur;

        cur.prev.next = node;
        cur.prev = node;
    }

    public int size() {
        Node cur = head;
        int size = 0;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        return size;
    }

    public boolean contains(int data) {
        Node cur = head;
        while (cur != null) {
            if (cur.val == data) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public boolean remove(int date) {
        if (head == null) {
            return false;
        }
        Node cur = head;
        while (cur != null) {
            if (cur.val == date) {
                if (cur == head) {
                    head = head.next;
                    if (head == null) {
                        last = null;
                    }else {
                        head.prev = null;
                    }
                }else {
                    if (cur == last) {
                        last = last.prev;
                        last.next = null;
                    }else {
                        cur.prev.next = cur.next;
                        cur.next.prev = cur.prev;
                    }
                }
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    public void removeAll(int data) {
        if (head == null) {
            return;
        }
        Node cur = head;
        while (cur != null) {
            if (cur.val == data) {
                if (cur == head) {
                    head = head.next;
                    if (head == null) {
                        last = null;
                    }else {
                        head.prev = null;
                    }
                }else if (cur == last){
                    last = last.prev;
                    last.next = null;
                }else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
            }
            cur = cur.next;
        }
    }

    public void clear() {
        Node cur = head;
        while (cur != null) {
            Node curNext = cur.next;
            cur.prev = null;
            cur.next = null;
            cur = curNext;
        }
        head = last = null;
    }

    public void print() {
        Node cur = head;
        while (cur != null) {
            System.out.print(cur.val + " , ");
            cur = cur.next;
        }
        System.out.println();
    }
    public static void main(String[] args) {
        DoubleLinkedList linkedList = new DoubleLinkedList();
        linkedList.addFirst(1);
        linkedList.addLast(2);
        linkedList.addLast(2);
        linkedList.addLast(2);
        linkedList.addIndex(9,1);

        linkedList.print();
        linkedList.remove(2);
        linkedList.print();
        linkedList.removeAll(2);
        linkedList.print();
    }
}
